#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <vector>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <ultoa.h>



// my functions
#include <open_stream_name_with_subscript.h>
#include <print_error.h>
#include <program_options_helper.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;

//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("sample_positives,s", po::value<unsigned>(&args.sample_hits)
										->set_name("INTEGER"),
									"The number of samples which matched.")
	("sample_population,S", po::value<unsigned>(&args.sample_pop)
										->set_name("INTEGER"),
									"The number of samples altogether.")
	("background_positives,b", po::value<unsigned>(&args.total_hits)
										->set_name("INTEGER"),
									"The total number of all (background) items which matched.")
	("background_population,B", po::value<unsigned>(&args.total_pop)
										->set_name("INTEGER"),
									"The total number of all (background) items.");
	

	args.optional_args.add_options()
	("fewer_than_sample,f",		po::bool_switch(&args.less_than), 	
									"The probability of underrepresentation "
									"(hits <= --sample_hits).");

	args.add_std_options();
	po::options_description all_args("");
	all_args
				.add(mandatory_args)
				.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);



	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0");
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
				"Calculates the Hypergeometric Cummulative Distribution Function "
				"using routines from the GSL library. ";
		print_usage(cerr, string(*argv), "--sample_hits           INTEGER\n"
										 "--sample_population     INTEGER\n"
										 "--background_hits       INTEGER\n"
										 "--background_population INTEGER\n"
										 "[less_than_sample]\n"
										 "> PROBABILITY",
							all_args, exe_description, 80);
		return false;
	}
	check_required_options(vm, mandatory_args);

	args.open_err_log();

	if (args.sample_hits > args.sample_pop)
		throw std::runtime_error("The number of sample which hit (--sample_hits = " +
								 ultoa(args.sample_hits) + " ) "
								"has to be smaller than the number of samples ("
								"--sample_population = " + ultoa(args.sample_pop) + " ) ");
	if (args.total_hits > args.total_pop)
		throw std::runtime_error("The number of all items which hit (--background_hits = " +
								 ultoa(args.total_hits) + " ) "
								"has to be smaller than the background population size ("
								"--background_population = " + ultoa(args.total_pop) + " ) ");
	if (args.sample_pop > args.total_pop)
		throw std::runtime_error("The number of items sampled (--sample_population = " +
								 ultoa(args.sample_pop) + " ) "
								"has to be smaller than the total (background) population ("
								"--background_population = " + ultoa(args.total_pop) + " ) ");


	return true;
}








